<?php
error_reporting(0);
/**
 * GameController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Wap_AuthController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (!$this->_arrParam['page']) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (!$this->_arrParam['line_per_page']) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
    }

    public function preDispatch(){
		$template_path = TEMPLATE_PATH . "/wap/default/";
		$this->loadTemplate($template_path,'template.ini','template');
    }
    /**
     * The default action - show the home page
     */
    public function indexAction (){
    }
    public function createSessionAction(){
    	
    	Zend_Loader::loadClass('Wap_Models_UserModel');
    	Zend_Loader::loadClass('Wap_Models_UserInfoModel');
    	
    	$param = explode('&', $this->aes128Decrypt(AES_KEY, base64_decode($this->_arrParam['param'])));
//    	Zend_Debug::dump($this->_arrParam);
//    	Zend_Debug::dump($param);die;
//    	Zend_Debug::dump(explode(',', $this->_arrParam['installedApps']));die;
//    	echo urlencode(base64_encode($this->aes128Encrypt(AES_KEY, "1&thaind@sunnet.vn&3&320&/wap&123")));
//    	die;
    	if ($param){
    		if ($param[1]){
    			$user_model = new Wap_Models_UserModel();
    			$user_info_model = new Wap_Models_UserInfoModel();
    			$check_user = $user_model->checkUser(array('type'	=> 'email','email'	=> $param[1]));
//				Zend_Debug::dump($check_user);die;
    			if ($check_user){
    				$auth = Zend_Auth::getInstance();
    				$auth->getStorage()->write(array('UserID'	=> $check_user['UserID']));
		            Zend_Session::rememberMe();
//    			}else{
//    				$code = $this->generateCode(10);
//					$client = new Zend_Http_Client(SCGW_SERVER . '/user/create-user');
//					$client->setParameterGET(array(
//						'user_id'	=> $code,
//						'username'	=> trim($this->_arrParam['username']),
//						'password'   	=> trim($this->_arrParam['password'])
//					));
//    				try {
//				    	$response = $client->request('GET');
//				    	$check_create_user = explode('|', $response->getBody());
//				    	if ($check_create_user[0] == 0){
//				    		try {
//						    	$user_model->insert(array('userID'			=> $code,
//						    								'email'			=> $this->_arrParam['email'],
//						    								'password'		=> $this->_arrParam['password'],
//						    								'EncryptedPassword'		=> sha1($this->_arrParam['password'])));
//						    	$user_info_model->insert(array('userID'		=> $code,
//						    									'email'		=> $this->_arrParam['email'],
//						    									'Username'	=> $this->_arrParam['username'],
//						    									'via'	=> $this->_arrParam['via']));
//				    		} catch (Exception $e) {
//				    		}
//				    	}
//			    	}catch (Exception $e){
//			    		
//			    	}
    			}
    		}

            // check user vip
            Zend_Loader::loadClass('Wap_Models_AccountModel');
            $mAccount = new Wap_Models_AccountModel();
            //check xem có pai la user vip
            $checkVip = $mAccount->checkUserVipIsReturnTrue($this->id_user);
            
            //end check
    		$session = new Zend_Session_Namespace();
    		$session->channel_id = $param[0];
    		$session->email = $param[1];
    		$session->product_id = $param[2];
    		$session->device_width = $param[3];
			$session->current_app_packet = $this->_arrParam['currentApp'];
			$session->installed_app_packet = explode(',', $this->_arrParam['installedApps']);

            $session->game_coin = $param[6];
            $session->game_exchange = $param[7];
            
            //session check user vip
            $session->user_vip = $checkVip;
            
    		if (isset($param[4])){
    			$this->_redirect($param[4]);
    		}
    	}
    }
	function aes128Encrypt($key, $data) {
		$block = mcrypt_get_block_size('rijndael_128', 'ecb');
		$pad = $block - (strlen($data) % $block);
		$data .= str_repeat(chr($pad), $pad);
		return (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB));
	}
	function aes128Decrypt($key, $data) {
		//$data = base64_decode($str);
		$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);
		$block = mcrypt_get_block_size('rijndael_128', 'ecb');
		$pad = ord($data[($len = strlen($data)) - 1]);
		$len = strlen($data);
		$pad = ord($data[$len-1]);
		return substr($data, 0, strlen($data) - $pad);
	}
	function generateCode($characters) {
		// $characters = how many digits or characters to return.
		// You can use any set of characters you want.
		$possible = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$code = '';
		$i = 0;
		while ($i < $characters) { 
			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
			$i++;
		}
		return $code;
	}
}
